package com.comp490.sound;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

import com.comp490.shooter.R;

public class SoundManager
{
    public static final String SOUND_PREFS = "sound preferences";
    public static final String FX_ON = "sound effects on";
    public static final String MUSIC_ON = "music on";

    private static boolean soundFxMuted;
    private static boolean musicMuted;

    private static Context context;
    private static SoundPool soundPool;
    private static MediaPlayer mediaPlayer;
    private static int length;
    private static AudioManager audioManager;

    public static SoundManager instance;
    public static int PLAYER_FIRE;
    public static int PLAYER_EXPLOSION;
    public static int ENEMY_EXPLOSION;
    public static int BIG_ENEMY_EXPLOSION;
    public static int SWAP_MENU;
    public static int BUTTON_CLICK;
    public static int SCORE_BEEP;
    
    public static int LEVEL_ONE_BG;
    public static int MAIN_MENU_BG;
    public static int SCORE_BG;

    

    public SoundManager(Context context)
    {
        SoundManager.context = context;
        initSoundManager();
    }

    public void initSoundManager()
    {
        // creates the pool with 10 simultaneous sounds playing. stream_music is
        // typical for games, quality 0 by default;
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        // load resources
        ENEMY_EXPLOSION = soundPool.load(context, R.raw.explosion, 1);
        PLAYER_FIRE = soundPool.load(context, R.raw.ship_fire, 1);
        PLAYER_EXPLOSION = soundPool.load(context, R.raw.player_explosion, 1);
        SWAP_MENU = soundPool.load(context, R.raw.hatches, 1);
        BUTTON_CLICK = soundPool.load(context, R.raw.double_beep_short, 1);
        SCORE_BEEP = soundPool.load(context, R.raw.score_chime, 1);
        BIG_ENEMY_EXPLOSION = soundPool.load(context, R.raw.big_explosion, 1);
        MAIN_MENU_BG = R.raw.menu_music;
        LEVEL_ONE_BG = R.raw.level_one_music;
        SCORE_BG = R.raw.score_beat;
    }

    public static void playBackgroundMusic(int soundID)
    {
        assert context != null;

        if(mediaPlayer != null)
            mediaPlayer.reset();

        audioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);

        mediaPlayer = MediaPlayer.create(context, soundID);
        mediaPlayer.setVolume(100f, 100f);
        mediaPlayer.setLooping(true);

        length = -1;
        if(!musicMuted)
            mediaPlayer.start();
    }

    public static void pauseBackgroundMusic()
    {
    	length = -1;
        if(mediaPlayer != null && mediaPlayer.isPlaying())
        {
            mediaPlayer.pause();
            length = mediaPlayer.getCurrentPosition();
        }
    }

    /**
     * resumes play of currently instantiated mediaPlayer. Does nothing
     * otherwise
     */
    public static void resumeBackgroundMusic()
    {
        if(!musicMuted && mediaPlayer != null && !mediaPlayer.isPlaying())
        {
        		if(length != -1)
        			mediaPlayer.seekTo(length);
                mediaPlayer.start();
        }
    }

    // public static void stopBackgroundMusic()
    // {
    // mediaPlayer.release();
    // }

    public static void playSoundEfx(int soundID)
    {
        assert context != null;

        if(!soundFxMuted)
        {
            audioManager = (AudioManager) context
                    .getSystemService(Context.AUDIO_SERVICE);
            float volume = audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC);
            soundPool.play(soundID, volume, volume, 1, 0, 1f);
        }
    }

    public static void muteSoundFx()
    {
        soundFxMuted = true;
    }

    public static void unMuteSoundFx()
    {
        soundFxMuted = false;
    }

    public static void muteMusic()
    {
        pauseBackgroundMusic();
        musicMuted = true;
    }

    public static void unMuteMusic()
    {
        if(mediaPlayer != null && !mediaPlayer.isPlaying())
            mediaPlayer.start();

        musicMuted = false;
    }

    public static SoundManager getInstance(Context context)
    {
        if(instance == null)
            instance = new SoundManager(context);

        // Maybe new activity asking for this same instance? Make it the context
        // Share resources
        if(!SoundManager.context.equals(context))
            SoundManager.context = context;

        return instance;
    }
}
